#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
#include "strlib.h"

#define NWEIGHTS 10

bool IsMeasurable(int target, int weights[], int nWeights);
void printMeasurable(int weights[], int nWeights);
int getTotalSumOfWeights(int weights[], int nWeights);

main (){
	int weights[NWEIGHTS];
	int nWeights, num;

	printf("Give available weights, finish with -1\n");
	for(nWeights = 0; nWeights < NWEIGHTS; nWeights++){
		num = GetInteger();

		if(num >= 0)
			weights[nWeights] = num;
		else
			break;
	}
	
	printMeasurable(weights, nWeights);
}

int getTotalSumOfWeights(int weights[], int nWeights){
	int i, totSum = 0;

	for(i = 0; i < nWeights; i++){
		totSum += weights[i];
	}
	return totSum;
}

void printMeasurable(int weights[], int nWeights){
	int i,sumOfWeights;

	sumOfWeights = getTotalSumOfWeights(weights,nWeights);

	for(i = 1; i <= sumOfWeights; i++){
		printf("%d is", i);

		if(!IsMeasurable(i, weights, nWeights))
			printf(" NOT ");

		printf("measurable.\n");
	}
}

bool IsMeasurable(int target, int weights[], int nWeights){
	if(nWeights == 0){
		return FALSE;
	}
	else{
		if(target == weights[nWeights-1] || target == 0) 
			return TRUE;

		return	IsMeasurable(target,weights,nWeights -1) ||
				IsMeasurable(target+weights[nWeights-1],weights,nWeights -1) ||
				IsMeasurable(target-weights[nWeights-1],weights,nWeights -1);
	}
}
